package client.service;

import java.io.DataInputStream;

import java.io.DataOutputStream;
import java.util.Vector;
import java.lang.reflect.Type;
import model.Message;
import model.MsgType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class HandleMessage {
	private DataInputStream dis;
	private DataOutputStream dos;
	public HandleMessage(DataInputStream dis, DataOutputStream dos) {
		this.dis = dis;
		this.dos = dos;
	}
    public Vector<model.Message> initHistory(int userId, int roomId) {
		Vector<model.Message> history = new Vector<>();
		try {
			Message msg = new Message();
            msg.setUserID(userId);
            msg.setRoomID(roomId);
            msg.setType(MsgType.HISTORY_MESSAGE);
            Gson gson = new Gson();
            String json = gson.toJson(msg);
            dos.writeUTF(json);
            String retmes = dis.readUTF();
            Type messageType = new TypeToken<Vector<model.Message>>() {}.getType();
            history = gson.fromJson(retmes, messageType);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return history;
    }
    public Vector<model.Message> initMachineHistory(int userId) {
		Vector<model.Message> history = new Vector<>();
		try {
			Message msg = new Message();
            msg.setUserID(userId);
            msg.setType(MsgType.MACHINE_HISTORY);
            Gson gson = new Gson();
            String json = gson.toJson(msg);
            dos.writeUTF(json);
            String retmes = dis.readUTF();
            Type messageType = new TypeToken<Vector<model.Message>>() {}.getType();
            history = gson.fromJson(retmes, messageType);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return history;
    }
}

